package hu;

import java.util.*;

public class task {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int n = sc.nextInt();
            int[][] data = new int[n][2];
            int[] dp = new int[n];
            int last = 0;
            int res = 0;
            for (int j = 0; j < n; j++) {
                data[j][0] = sc.nextInt();
                data[j][1] = sc.nextInt();
            }
            Arrays.sort(data, (a,b) -> (b[1] - a[1]));
            for (int j = 0; j < n; j++) {
                dp[j] = last + data[j][0] + data[j][1];
                last += data[j][0];
                res = Math.max(res, dp[j]);
            }
            System.out.println(res);
        }
    }
}
